local xinmashikong = fk.CreateSkill{
    name = "rmt__xinmashikong",
}
Fk:loadTranslationTable{
    ["rmt__xinmashikong"] = "心码失控",
    [":rmt__xinmashikong"] = "游戏开始时或你获得此技能时，你打乱其他角色的技能描述。每名角色出牌阶段限一次，其可以交给你一张手牌，令其本回合可以观看所有角色技能的原描述。",
    ["@[xinmashikong]rmt__xinmashikong-phase"] = "正常描述",
}

local TAG_PATTERN = "<[^>]+>"
-- 分割字符串为文本块和标签块
local function split_text_and_tags(s)
    local parts = {}
    local last_end = 1

    -- 遍历所有标签匹配项
    for tag_start, tag_end in s:gmatch("()"..TAG_PATTERN.."()") do
        -- 提取标签前的文本
        if tag_start > last_end then
            local text = s:sub(last_end, tag_start - 1)
            table.insert(parts, {type="text", content=text})
        end
        -- 提取标签
        local tag = s:sub(tag_start, tag_end - 1)
        table.insert(parts, {type="tag", content=tag})
        last_end = tag_end
    end

    -- 处理末尾的剩余文本
    if last_end <= #s then
        local text = s:sub(last_end)
        table.insert(parts, {type="text", content=text})
    end

    return parts
end
-- UTF-8 安全分割函数
local function utf8_split(s)
    local chars = {}
    local i = 1
    while i <= #s do
        local c = string.byte(s, i)
        local shift = 1
        if c >= 0xF0 then  -- 4字节字符
            shift = 4
        elseif c >= 0xE0 then  -- 3字节字符（如中文）
            shift = 3
        elseif c >= 0xC0 then  -- 2字节字符
            shift = 2
        end
        table.insert(chars, string.sub(s, i, i + shift - 1))
        i = i + shift
    end
    return chars
end
-- Fisher-Yates 洗牌算法
local function shuffle(t)
    for i = #t, 2, -1 do
        local j = math.random(i)
        t[i], t[j] = t[j], t[i]
    end
    return t
end
local function shuffle_with_keep_tags(s)
    local parts = split_text_and_tags(s)
    local result = {}

    for _, part in ipairs(parts) do
        if part.type == "text" then
            -- 分割并打乱文本部分
            local chars = utf8_split(part.content)
            --[[if #result > 1 then
                table.insert(result, math.random(#result), table.concat(shuffle(chars)))
            else]]--
                table.insert(result, table.concat(shuffle(chars)))
            --end
        else
            -- 直接保留标签
            table.insert(result, part.content)
        end
    end

    return table.concat(result)
end

xinmashikong:addEffect(fk.GameStart, {
    can_trigger = function (self, event, target, player, data)
        return player:hasSkill(xinmashikong.name)
    end,
    on_cost = Util.TrueFunc,
    on_use = function (self, event, target, player, data)
        local room = player.room
        for _, cp in ipairs(room:getOtherPlayers(player)) do
            local translate, skills = {}, {}
            for _, skill in ipairs(cp.player_skills) do
                local name = skill.name
                local str = Fk:translate(":"..name,"zh_CN")
                if str and str ~= "" and not skill:isEquipmentSkill(cp) then
                    str = shuffle_with_keep_tags(str)
                    str = "<b><font color='#9fd49c' weight='bold' size='4'>" .. Fk:translate(name,"zh_CN") .. "</font></b> <font color='#E4D5A0'>" .. str .. "</font>"
                    --Fk.translations["zh_CN"][":"..name] = str
                    table.insertIfNeed(translate,str)
                    table.insertIfNeed(skills,name)
                end
            end
            if #skills > 0 then
                room:handleAddLoseSkills(cp,"-"..table.concat(skills,"|-"),nil,false,true)
            end
            room:handleAddLoseSkills(cp,"rmt__xinmashikong_translate",xinmashikong.name,false,true)
            if #translate > 0 then
                table.insert(translate,1,"该角色技能描述已被"..Fk:translate(player.general,"zh_CN").."打乱。出牌阶段限一次，你可以交给其一张手牌，令你本回合可以观看所有角色技能的原描述。")
                room:setPlayerMark(cp,"rmt__xinmashikong_translate",translate)
            end
            if #skills > 0 then
                room:handleAddLoseSkills(cp,table.concat(skills,"|"),nil,false,true)
            end
        end
    end,
})

xinmashikong:addAcquireEffect(function (self, player, is_start)
    local room = player.room
    for _, cp in ipairs(room:getOtherPlayers(player)) do
        local translate, skills = cp:getTableMark("rmt__xinmashikong_translate"), {}
        for _, skill in ipairs(cp.player_skills) do
            local name = skill.name
            local str = Fk:translate(":"..name,"zh_CN")
            if str and str ~= "" and not skill:isEquipmentSkill(cp) then
                str = shuffle_with_keep_tags(str)
                str = "<b><font color='#9fd49c' weight='bold' size='4'>" .. Fk:translate(name,"zh_CN") .. "</font></b> <font color='#E4D5A0'>" .. str .. "</font>"
                --Fk.translations["zh_CN"][":"..name] = str
                table.insertIfNeed(translate,str)
                table.insertIfNeed(skills,name)
            end
        end
        if #skills > 0 then
            room:handleAddLoseSkills(cp,"-"..table.concat(skills,"|-"),nil,false,true)
        end
        room:handleAddLoseSkills(cp,"rmt__xinmashikong_translate",xinmashikong.name,false,true)
        if #translate > 0 then
            table.insert(translate,1,"该角色技能描述已被"..Fk:translate(player.general,"zh_CN").."打乱。出牌阶段限一次，你可以交给其一张手牌，令你本回合可以观看所有角色技能的原描述。")
            room:setPlayerMark(cp,"rmt__xinmashikong_translate",translate)
        end
        if #skills > 0 then
            room:handleAddLoseSkills(cp,table.concat(skills,"|"),nil,false,true)
        end
    end
end)

return xinmashikong